export default class Common extends Laya.Script {

    static instance: Common
    static urlsOne = [
        [0,1,2,3,4,1,3,4,1],
        [3,2,1,3,4,1,2,4,2],
        [0,4,2,1,4,1,3,3,1],
        [2,1,0,3,4,0,3,2,1],
        [1,3,2,2,4,1,0,3,1],
    ]
    static urlsTwo = [
        [0,1,2,3,4,1,3,4,1,2,0,1],
        [3,2,1,3,4,1,2,4,2,3,4,0],
        [0,4,2,1,4,1,3,3,1,2,4,0],
        [2,1,0,3,4,0,3,2,1,0,4,0],
        [1,3,2,2,4,1,0,3,1,2,3,0],
    ]
    static isMove//绳子移动
    static isGameOver//是否结束游戏
    static istaobao = true;
    static type = 1//皮肤；

 
    constructor() {
         super();
         Common.instance = this;
         Laya.MouseManager.multiTouchEnabled = false;
         }

    static doingThings(thing: Function, isdo) {
        if (isdo) {
            thing();
        }
    }

    static getInstance() {
        if (!Common.instance) {
            Common.instance = new Common()
        }
        return Common.instance
    }
    static randomInt(min:number,max:number){
        let value = Math.floor(Math.random()*(max-min+1)+min);
        return value;
    }
     /* -------------------------------------------淘宝功能 ------------------------------------------------------------------*/


    //外跳链接
    static jumpOutUrl(url: string) {
        my.navigateToOutside({
            url,
            success: (res) => {
                console.log(1111, JSON.stringify(res))
            },
            fail: (err) => {
                console.log(2222, JSON.stringify(err))
            }
        })
    }
    //商品详情
    static 	Order(itemId: string) {
		my.tb.openDetail({
			itemId: itemId,
			success: (res) => {
				// my.alert({ content: "success" });
			},
			fail: (res) => {
				my.alert({ content: "fail - " + res.error });
			},
		});
	}
    //商品加购
    static 	Purchased_mianmo(itemId: string) {
		my.tb.showSku({
			itemId: itemId,
			success: (result) => {
				// 这里把用户选中数据返回给业务方处理，添加到小购物车。和天猫超市类似，选中宝贝也会同步在手淘购物车展示  
				my.showToast({
					content: 'showSku ' + JSON.stringify(result)
				});
			}
		});
	}
    //前往店铺首页
    static Orders(shopId: string) {
        console.log('前往店铺')
        my.navigateToTaobaoPage({
            appCode: 'shop',
            appParams: {
                shopId: shopId,
                weexShopTab: "shopindexbar"
            },
            success: (res) => {
                console.log(1111, JSON.stringify(res))
            },
            fail: (res) => {
                console.log(2222, JSON.stringify(res))
            }
        });
	}
    //分享好友
    static showSharePanl(shopId: string){
        my.showSharePanel({
            query: {
                shopId:shopId,
                address:'hangzhou'
            },
            title: '测试标题',
            imageUrl: 'https://gw.alicdn.com/bao/upload/bao/i2/670988855/O1CN01cDLGb42FHdqYmh2hX_!!670988855.jpg_570x10000q50.jpg',
            success: function(res) {
              my.alert({content: '成功：' + res});
            },
            fail: function(res){
              my.alert({content: '失败：' + res});
            }
          });
    }
    //分享回调
    static getSceneInfo(){
        my.getSceneInfo({
			success: function(res) {
			let params = res.widgetShareParams;
			//console.log(params.address);
			  my.alert({content: '获取成功：' + params});
			},
			fail: function(res){
			  my.alert({content: '获取失败：' + res});
			}
		});
    }



 /* -------------------------------------------资源加载 -------------------------------------------------------------------*/
    //动态加载图片
    static loadImage(url: string, posx: number, posy: number, width: number, height: number, parent: Laya.Node) {
        let img: Laya.Image = new Laya.Image();
        parent.addChild(img);
        img.pos(posx, posy);
        img.width = width;
        img.height = height;
        //加载显示图片，坐标位于100,50
        img.loadImage(url);
        return img
    }
    //动态加载文本
    static loadTxt(pos_x: number, pos_y: number, width: number, height: number, align: string, valign: string, wordWrap,parent:Laya.Node) {
        let txt: Laya.Text = new Laya.Text();
        txt.pos(pos_x, pos_y);
        parent.addChild(txt);
        txt.width = width;
        txt.height = height;
        txt.align = align;
        txt.valign = valign;
        txt.wordWrap = wordWrap;
        return txt
    }
    //动态加载预制体
    static loadPrefab(jsonUrl: string, name: string, parent: Laya.Node) {
        Laya.loader.load(jsonUrl, Laya.Handler.create(this, function (pref: Laya.Prefab) {
            let newPrefab = new Laya.Prefab;
            newPrefab.json = pref;
            var prefab = Laya.Pool.getItemByCreateFun(name, newPrefab.create, newPrefab);
            parent.addChild(prefab);
        }))
    }
/* -------------------------------------------实现方法 -------------------------------------------------------------------*/
    //设置节点数组
    static GetChildNodesArray(target:Laya.Node)
    {
        let nodeArray:Laya.Node[]=[];
        for(let i=0;i<target.numChildren;i++){
            let node = target.getChildAt(i);
            if(node){
                nodeArray.push(node);
            }
        }
        return nodeArray;
    }
    
    //获取小部件关联小程序信息
    static GetRelationAppInfo() {
        my.getRelationAppInfo({
            success: (res) => {
                // console.log(JSON.stringify(res));
                return JSON.stringify(res)
            },
            fail: (res) => {
                console.log(JSON.stringify(res));
            },
        });
        
    }
   //获取关联的小程序ID
    static GetRelationAppId() {
        my.getRelationAppId({
            success: (res) => {
                console.log(JSON.stringify(res.relationAppId));
            },
            fail: (res) => {
                console.log(JSON.stringify(res));
            },
        });
    }

   //用户授权
    static Authorize(){
        my.authorize({
            scopes: 'scope.userInfo',
            success: (res) => {
              my.alert({
                content: JSON.stringify(res),
              });
            },
          });
    }

    //收藏商品和取消收藏商品
    static CollectGoods(Id: number, isok: boolean) {
        if (isok) {
            my.tb.collectGoods({
                id: Id,  //商品id
                success: (res) => {
                    my.alert({ content: "success - " + JSON.stringify(res) })
                },
                fail: (res) => {
                    my.alert({ content: "fail - " + JSON.stringify(res) })
                },
                complete: (res) => {
                    my.alert({ content: "complete - " + JSON.stringify(res) })
                }
            })
        } else {
            my.tb.unCollectGoods({
                id: Id,
                success: (res) => {
                    my.alert({ content: "success - " + JSON.stringify(res) })
                },
                fail: (res) => {
                    my.alert({ content: "fail - " + JSON.stringify(res) })
                },
                complete: (res) => {
                    my.alert({ content: "complete - " + JSON.stringify(res) })
                }
            })
        }

    }











}